<?php
namespace app\admin\controller\sys;

use app\admin\controller\Base;

use think\facade\Request;
use think\facade\Session;
use think\facade\View;
use app\admin\model\sys\AuthRule;


class AuthGroup extends Base
{
    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\admin\model\sys\AuthGroup;
        $this->redict = url('index');
    }

    // 用户组管理
    public function index()
    {
        if(Request::isPost()){
            $keys = Request::param('title');
            $option = [
                'where' => [
                    ['title','like',"%".$keys."%"]
                ]
            ];
            $list = $this->model->loadList($option);
            if ($list['result'] == 'success'){
                $result = ['code' => 0, 'msg' => lang('get info success'), 'data' => $list['list'], 'count' => $list['page']['total']];
            }else{
                $result = ['code' => 0, 'msg' => lang('get info success'), 'data' => [], 'count' => 0];
            }
            return $result;
        }
        return view();
    }



    // 用户组添加
    public function add()
    {
        if (Request::isPost()) {
            return parent::create();
        } else {
            return parent::add();
        }
    }

    // 用户组修改
    public function edit()
    {
        if (Request::isPost()) {
            return parent::modify();
        } else {
            return parent::read('add');
        }
    }

    // 用户组显示权限
    public function access()
    {
        $admin_rule = AuthRule::field('id, pid, title as label, href')
            ->where('status',1)
            ->order('sort asc')->cache(3600)
            ->select()->toArray();
        $rules = $this->model::where('id', Request::param('id'))
            ->where('status',1)
            ->value('rules');
        $list = AuthRule::authChecked($admin_rule, $pid = 0, $rules);
        $group_id = Request::param('id');
        $idList = AuthRule::column('id');
        // print_r($idList);die;
        sort($idList);

        $view = [
            'list' => $list,
            'idList' => $idList,
            'group_id' => $group_id,
        ];
        View::assign($view);
        return view();
    }

    // 用户组保存权限
    public function setaccess()
    {
        $rules = $this->request->post('rules');
        if (empty($rules)) {
            $this->error(lang('please choose rule'));
        }
        $data = $this->request->post();
        $rules = AuthRule::authNormal($rules);
        $rls = '';
        foreach ($rules as $k=>$v){
            $rls.=$v['id'].',';
        }
        $where['id'] = $data['group_id'];
        $where['rules'] = $rls;
        if ($this->model::update($where)) {
            $admin = Session::get('admin');
            $admin['rules'] = $rls;
            Session::set('admin', $admin);
            $this->success(lang('rule assign success'),url('sys.AuthGroup/index'));
        } else {
            $this->error(lang('rule assign fail'));
        }
    }


}